package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.companyinfo.MultiBoundCompanyException;
import com.xinzhitong.www.pojo.Company;
import com.xinzhitong.www.service.CompanyUserInfoService;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 企业用户服务控制器
 *
 * @author 徐鑫 吴展鹏
 */
@CrossOrigin
@RestController
@RequestMapping("companyUserInfo")
public class CompanyUserInfoController {

    @Resource
    CompanyUserInfoService companyUserInfoService;

    /**
     * 完善企业资料
     *
     * @param logo         logo
     * @param name         全称
     * @param shortname    简称
     * @param nature       性质/类型
     * @param industry     行业
     * @param date         成立时间
     * @param scale        规模
     * @param province     省
     * @param city         市
     * @param area         区
     * @param address      详细地址
     * @param contact      联系方式
     * @param introduction 介绍
     * @param pictures     图片列表
     * @return 响应信息
     */
    @RequiresRoles(value = {"COMPANY"})
    @ResponseBody
    @RequestMapping(value = "infoCreation", method = RequestMethod.PUT)
    public Message infoCreation(
            @RequestParam("logo") MultipartFile logo,
            @RequestParam("name") String name,
            @RequestParam("shortname") String shortname,
            @RequestParam("nature") String nature,
            @RequestParam("industry") String industry,
            @RequestParam("date") String date,
            @RequestParam("scale") Integer scale,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("area") String area,
            @RequestParam("address") String address,
            @RequestParam("contact") String contact,
            @RequestParam("introduction") String introduction,
            @RequestParam("pictures") MultipartFile[] pictures
    ) {
        String msg = "修改信息成功";
        Company company = new Company(
                name,
                shortname,
                nature,
                industry,
                date,
                scale,
                province,
                city,
                area,
                address,
                contact,
                introduction
        );
        try {
            companyUserInfoService.createCompanyInfo(company, logo, pictures);
            return Message.createSuc(msg);
        } catch (MultiBoundCompanyException e) {
            e.printStackTrace();
            msg = "您已创建过公司信息，请勿重复创建";
        }
        return Message.createErr(msg);
    }


    /**
     * 完善企业资料
     *
     * @param logoNum      logo数量
     * @param name         全称
     * @param shortname    简称
     * @param nature       性质/类型
     * @param industry     行业
     * @param date         成立时间
     * @param scale        规模
     * @param province     省
     * @param city         市
     * @param area         区
     * @param address      详细地址
     * @param contact      联系方式
     * @param introduction 介绍
     * @param picturesNum  图片数量
     * @return 响应信息
     */
    @RequiresRoles(value = {"COMPANY"})
    @ResponseBody
    @RequestMapping(value = "infoCreationAcceleratePictures", method = RequestMethod.PUT)
    public Message infoCreationAcceleratePicture(
            @RequestParam("logoNum") Integer logoNum,
            @RequestParam("name") String name,
            @RequestParam("shortname") String shortname,
            @RequestParam("nature") String nature,
            @RequestParam("industry") String industry,
            @RequestParam("date") String date,
            @RequestParam("scale") Integer scale,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("area") String area,
            @RequestParam("address") String address,
            @RequestParam("contact") String contact,
            @RequestParam("introduction") String introduction,
            @RequestParam("picturesNum") Integer picturesNum
    ) {
        String msg = "修改信息成功";
        Company company = new Company(
                name,
                shortname,
                nature,
                industry,
                date,
                scale,
                province,
                city,
                area,
                address,
                contact,
                introduction
        );
        try {
            List<Map<String,Object>> x = companyUserInfoService.createCompanyInfoAcceleratePicture(company, logoNum, picturesNum);
            return Message.createSuc(x);
        } catch (MultiBoundCompanyException e) {
            e.printStackTrace();
            msg = "您已创建过公司信息，请勿重复创建";
            return Message.createErr(msg);
        }
    }


    /**
     * 获取首页概要
     *
     * @return 首页概要三个重要信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @ResponseBody
    @RequestMapping(value = "companyAccountView", method = RequestMethod.GET)
    public Message companyAccountView() {
        return Message.createSuc(companyUserInfoService.getView());
    }

    /**
     * 获取账号信息
     *
     * @return 账号信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @ResponseBody
    @RequestMapping(value = "companyUserInfo", method = RequestMethod.GET)
    public Message companyUserInfo() {
        return Message.createSuc(companyUserInfoService.getInfo());
    }

}
